package com.tronsis.distribution.activity.me;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ProductCodeFindBean;
import com.tronsis.distribution.biz.factory.ProductBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.permission.EasyPermissions;
import com.tronsis.library.util.LogUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.zxing.CaptureActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 扫码查询
 */
public class QrQueryActivity extends ToolBarBaseActivity {

    @BindView(R.id.et_code)
    EditText mEtCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_query);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.qr_query));
        mIBtnRight.setVisibility(View.VISIBLE);
        mIBtnRight.setImageResource(R.drawable.icon_qr_query);
    }


    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        if (paramId == RIGHT_BUTTON) {
            if (EasyPermissions.hasPermissions(mContext, Manifest.permission.CAMERA)) {
                Intent intent = new Intent(mContext, CaptureActivity.class);
                intent.setType("product");
                startActivityForResult(intent,0x50);
            } else {
                EasyPermissions.requestPermissions(mContext, getString(R.string.ask_again), 0x151, Manifest.permission.CAMERA);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x50 && resultCode == Activity.RESULT_OK) {
            String qrResult = data.getStringExtra(CaptureActivity.EXTRA_QR_RESULT);
            LogUtil.e("qrResult", "qrResult=" + qrResult);
            String[] split = qrResult.split("=");
            mEtCode.setText(split[1]);
        }
    }

    @OnClick(R.id.btn_find)
    void continueClick() {
        String code = mEtCode.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showMessage(mContext, "请输入产品码");
            return;
        }
        httpQuery(code);
    }


    private void httpQuery(String code) {
        try {
            ProductBizFactory.createBiz().findCodeDetail(this, code, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ProductCodeFindBean productCodeFindBean = (ProductCodeFindBean) data;
                    Intent intent = new Intent(mContext, QueryProductDetailActivity.class);
                    intent.putExtra("ProductCodeFindBean", productCodeFindBean);
                    startActivity(intent);
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

}
